/*
 * @Date: 2020-11-25 21:47:26
 * @LastEditors: 小枫
 * @description: description
 * @LastEditTime: 2020-11-26 21:23:56
 * @FilePath: \stu-nest\src\modules\hello\hello.controller.ts
 */
import {
  Controller,
  Get,
  Query,
  Headers,
  Post,
  Body,
  Param,
  UseFilters,
  HttpException,
  HttpStatus,
  ParseIntPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiBody, ApiParam, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { HelloService } from './hello.service';
import { Hello } from '../../classes/hello.dto';
import { HttpExceptionFilter } from 'src/common/filter/http-exception.filter';
import { RolesGuard } from '../../common/guards/roles.guard';
import { Roles } from 'src/common/decorators/roles.decorator';

@UseFilters(new HttpExceptionFilter())
@UseGuards(RolesGuard)
@Controller('/hello')
export class HelloController {
  constructor(private readonly helloService: HelloService) {}

  /**
   * 传入ID获取数据
   * @param id ID
   * @param UA user-agent
   */
  @Get('hi')
  @ApiQuery({ name: 'id', required: true })
  @ApiResponse({
    status: 200,
    description: 'get...',
    type: Hello,
  })
  @Roles('admin')
  hi(@Query() { id }, @Headers('user-agent') UA): string {
    console.log(UA);
    if (!id)
      throw new HttpException(
        {
          status: HttpStatus.BAD_REQUEST,
          message: '参数必填',
          error: 'id is must',
        },
        HttpStatus.BAD_REQUEST,
      );
    return this.helloService.getHi() + '——' + id;
  }
  @Post()
  @ApiBody({ description: '填写要发送的消息' })
  save(@Body() { message }): string {
    return '接收到消息:' + message;
  }
  @Get('id/:id')
  @ApiParam({ name: 'id', required: true })
  getId(@Param('id', new ParseIntPipe()) id: number): string {
    console.log(typeof id);
    return '接收到ID：' + id;
  }
}
